package com.yupi.oj.model.vo;

import cn.hutool.core.util.StrUtil;
import com.yupi.oj.model.entity.QuestionSubmitLog;
import com.yupi.oj.model.enums.ExecuteStatusEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.Optional;

/**
 * 做题提交记录表
 */
@ApiModel("做题提交记录表")
@Data
public class QuestionSubmitLogVO implements Serializable {

    @ApiModelProperty("id")
    private Long id;

    /**
     * 题目ID
     */
    @ApiModelProperty(value = "题目ID")
    private Long questionId;

    /**
     * 提交语言
     */
    @ApiModelProperty(value = "提交语言")
    private String submitLanguage;

    /**
     * 代码详情
     */
    @ApiModelProperty(value = "代码详情")
    private String codeDescription;

    /**
     * 内存消耗（KB）
     */
    @ApiModelProperty(value = "内存消耗（KB）")
    private Integer memoryConsume;

    /**
     * 执行时间（ms）
     */
    @ApiModelProperty(value = "执行时间（ms）")
    private Integer executeTime;

    /**
     * 代码长度(bytes)
     */
    @ApiModelProperty(value = "代码长度(bytes)")
    private Integer codeLength;

    /**
     * 提交结果状态；1.正确；2.错误；3.编译错误
     */
    @ApiModelProperty(value = "提交结果状态；1.正确；2.错误；3.编译错误")
    private String submitStatus;

    /**
     * 错误信息
     */
    @ApiModelProperty(value = "错误信息")
    private String errorInfo;

    /**
     * 创建用户
     */
    @ApiModelProperty(value = "创建用户")
    private String createBy;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    private static final long serialVersionUID = 1L;

    /**
     * Bean to VO
     *
     * @param questionSubmitLog 问题提交记录
     * @return QuestionSubmitLogVO
     */
    public static QuestionSubmitLogVO beanToVO(QuestionSubmitLog questionSubmitLog) {
        QuestionSubmitLogVO questionSubmitLogVO = new QuestionSubmitLogVO();
        BeanUtils.copyProperties(questionSubmitLog, questionSubmitLogVO);
        String executeStatusValue = ExecuteStatusEnum.getValueByKey(questionSubmitLogVO.getSubmitStatus());
        questionSubmitLogVO.setSubmitStatus(StrUtil.isBlank(executeStatusValue) ? "--" : executeStatusValue);
        questionSubmitLogVO.setExecuteTime(Optional.ofNullable(questionSubmitLogVO.getExecuteTime()).orElse(0));
        questionSubmitLogVO.setMemoryConsume(Optional.ofNullable(questionSubmitLogVO.getMemoryConsume()).orElse(0));
        return questionSubmitLogVO;
    }
}